﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace ScoreManagement
{
    public partial class SearchForm : Form
    {
        //保存查询字符串
        private string sql = " ";
        private string url = "server=127.0.0.1; port=3306;database=exercise;uid=root;pwd=123456";
        
        public SearchForm()
        {
            InitializeComponent();
        }

        private void SearchForm_Load(object sender, EventArgs e)
        {
            string _sql = "SELECT s_id as'学号',s_name as '姓名',s_class as '专业',s_sex as '性别',s_age as '出生日期',s_Allscore as '总学分',s_about as '备注' FROM student";
            MySqlConnection conn = new MySqlConnection(url);
            MySqlDataAdapter sda = new MySqlDataAdapter(_sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            stuDGV.DataSource = ds.Tables[0].DefaultView;
           // stuZY.SelectedIndex = 0;
        }
        private void MakeSqlStr()
        {
            //清空上次的查询字符串
            sql = "";
            if (stuXH.Text.Trim() != string.Empty)
            {
                sql = " and student.s_id='" + stuXH.Text.Trim()+"'";
            }
            if (stuXM.Text.Trim() != string.Empty)
            {
                sql += " and s_name like '%" + stuXM.Text.Trim() + "%'";
            }
            if (stuZY.Text != "所有专业")
            {
                sql += " and s_class='" + stuZY.Text + "'";
            }
        }

        private void search_btn_Click(object sender, EventArgs e)
        {

            //获取查询字符串
            MakeSqlStr();
            string _sql = "SELECT student.s_id as '学号',student.s_name as '姓名',student.s_class as '专业',s_sex as '性别',s_age as '出生日期',student.s_Allscore as '总学分',student.s_about as '备注' FROM student WHERE 1=1" + sql;
            MySqlConnection conn = new MySqlConnection(url);
            MySqlDataAdapter sda = new MySqlDataAdapter(_sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            stuDGV.DataSource = ds.Tables[0].DefaultView;
        }

        private void stuDGV_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string _sql = "SELECT student.s_name as '姓名',KCB.KCM as '课程',CJB.CJ as '成绩',KCB.XF as '学分' FROM student,KCB,CJB WHERE student.s_id=CJB.s_id " +
                           "and KCB.KCH=CJB.KCH and student.s_id='" + stuDGV.Rows[e.RowIndex].Cells[0].Value + "'";
            MySqlConnection conn = new MySqlConnection(url);
            MySqlDataAdapter sda = new MySqlDataAdapter(_sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            CourseForm courseFrm = new CourseForm();
            courseFrm.stuKCDGV.DataSource = ds.Tables[0].DefaultView;
            courseFrm.ShowDialog();
        }

        private void stuXH_TextChanged(object sender, EventArgs e)
        {

        }
       
    }
}